package com.android.lotto.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;

import com.android.lotto.R;
import com.android.lotto.adapter.MainPageAdapter;
import com.android.lotto.entity.TabEntity;
import com.android.lotto.fragment.UnionChoiceFragment;
import com.android.lotto.fragment.LottoResultFragment;
import com.android.lotto.fragment.SuperChoiceFragment;
import com.android.lotto.fragment.SettingFragment;
import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;

import java.util.ArrayList;

/**
 * Created by JS-00 on 2017/5/18.
 */

public class MainActivity extends BaseActivity {

    private String[] mTitles = {"双色球", "大乐透", "开奖", "我的"};
    private ArrayList<Fragment> mFragments = new ArrayList<>();
    private int[] mIconUnselectIds = {
            R.mipmap.tab_home_unselect, R.mipmap.tab_speech_unselect,
            R.mipmap.tab_contact_unselect, R.mipmap.tab_more_unselect};
    private int[] mIconSelectIds = {
            R.mipmap.tab_home_select, R.mipmap.tab_speech_select,
            R.mipmap.tab_contact_select, R.mipmap.tab_more_select};

    private CommonTabLayout mTabLayout_main;
    private ArrayList<CustomTabEntity> mTabEntities = new ArrayList<>();

    private ViewPager mViewPager_main;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lotto_main);

        for (int i = 0; i < mTitles.length; i++) {
            mTabEntities.add(new TabEntity(mTitles[i], mIconSelectIds[i], mIconUnselectIds[i]));
        }

        initFragments();

        mViewPager_main = (ViewPager)findViewById(R.id.vp_lotto_main);
        mViewPager_main.setAdapter(new MainPageAdapter(getSupportFragmentManager(), mTitles, mFragments));

        /** with ViewPager */
        mTabLayout_main = (CommonTabLayout)findViewById(R.id.tl_lotto_main);

        setTabLayout();
    }

    private void initFragments() {
        UnionChoiceFragment unionChoiceFragment = new UnionChoiceFragment();
        SuperChoiceFragment superChoiceFragment = new SuperChoiceFragment();
        LottoResultFragment lottoResultFragment = new LottoResultFragment();
        SettingFragment settingFragment = new SettingFragment();
        mFragments.add(unionChoiceFragment);
        mFragments.add(superChoiceFragment);
        mFragments.add(lottoResultFragment);
        mFragments.add(settingFragment);

    }

    private void setTabLayout() {
        mTabLayout_main.setTabData(mTabEntities);
        mTabLayout_main.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                mViewPager_main.setCurrentItem(position);
            }

            @Override
            public void onTabReselect(int position) {
            }
        });

        mViewPager_main.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                mTabLayout_main.setCurrentTab(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        //默认选中第一个
        mViewPager_main.setCurrentItem(0);
    }
}
